define(['WebPDF', './ToolHandlerManager'], function(WebPDF) {
	'use strict';
	WebPDF.provider('PDFApi', [function () {	
		this.pdfViewer = null;

		this.$get = ['$rootScope', 'ToolHandlerManager', function($rootScope, ToolHandlerManager) {
			var that = this;
			return {
				layout : function(data){							
					$rootScope.$apply(function(){
						$rootScope.viewerHeight = data.height;
						$rootScope.viewerWidth = data.width;							
					});
				},
				setScale: function(data){										
					$rootScope.$broadcast("setScale", data);						
				},
				gotoPage: function(data){
					$rootScope.$broadcast("gotoPage", data);		
				},
				nextPage: function(data) {
					$rootScope.$broadcast("nextPage", data);	
				},
				previousPage: function(data) {
					$rootScope.$broadcast("previousPage", data);	
				},
				
				setCurrentToolByName : function(toolName) {
					ToolHandlerManager.setCurrentToolByName(toolName);
				},
				curPageChanged: function(callback){
					if (typeof callback == "function") {
					 	that.pdfViewer.bind("curPageChanged", callback);
					}
					else{	
						setTimeout(function(){
							that.pdfViewer.trigger("curPageChanged", [callback]);
						}, 0);									
					}
				},
				pageCountChanged: function(callback){						
					if (typeof callback == "function") {
					 	that.pdfViewer.bind("pageCountChanged", callback);
					 }
					 else{
					 	setTimeout(function(){
					 		that.pdfViewer.trigger("pageCountChanged", [callback]);
					 	}, 0);					 	
					 } 
				},
				scaleChanged: function (callback) {							
					if (typeof callback == "function") {
						that.pdfViewer.bind("scaleChanged", callback); 
					}
					else {
						setTimeout(function(){
							that.pdfViewer.trigger("scaleChanged", [callback]);
						}, 0);						
					}
				}	
			};
		}];
	}]);
});